package me.hys.kits;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author: @author HYS . <br>
 * Date: 2015-9-19 下午3:30:15 . <br>
 * Function: html标签工具
 */
public class HtmlKit {
	public static final int LENGTH = 50;

	/**
	 * @Fields : HTML标签，空白符，&nbsp;全部替换
	 */
	public static String HTML_REG = "(<[^>]+>)|(&nbsp;)|(\t)";
	/**
	 * @Fields : 图片正则，用于过滤ueditor上传的图片
	 */
	public static String IMAGE_REG = "<img.*src=\"/health/(.+)\"\\s+(.+)/>";

	/**
	 * @Title : filter
	 * @Description : 过滤全部html标签
	 */
	public static String filter(String str) {
		return StrKit.isBlank(str) ? "" : str.replaceAll(HTML_REG, "");
	}

	/**
	 * @Title : filterImage
	 * @Description : 提取第一张图片
	 */
	public static String filterImage(String str) {
		Pattern iamgePatten = Pattern.compile(IMAGE_REG,
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = iamgePatten.matcher(str);
		if (matcher.find()) {
			return matcher.group(1);
		}
		return "";
	}

	/**
	 * @Title : filterSubstring
	 * @Description : 过滤全部html标签并且截取内容
	 */
	public static String filterSubstring(String str, int length) {
		if (StrKit.isBlank(str)) {
			return "";
		}
		String temp = filter(str);
		if (!StrKit.isBlank(temp) && temp.length() > length) {
			return temp.substring(0, length - 3) + "...";
		}
		return temp;
	}
}
